package com.mlan.userap.untils;

import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mlan.userap.AppConfig;
import com.mlan.userap.R;

public class ImageLoader {
    public static void show(ImageView imageView, String imageUrl) {
        show(imageView, imageUrl,0, false);
    }

    public static String completeUrl(String url) {
        return (url == null || url.indexOf("http://") != -1 || url.indexOf("https://") != -1) ? url : (AppConfig.imageBase + url);
    }

    public static void show(ImageView imageView, String imageUrl,int radius, boolean skipMemoryCache) {

        Log.e("ImageLoader", "show: "+imageUrl );
        int placeImg = R.drawable.img_blue;
        if(radius>0){
            Glide.with(imageView.getContext())
                    .asDrawable()
                    .skipMemoryCache(skipMemoryCache)
                    .load(completeUrl(imageUrl))
                    .thumbnail(0.5f)
                    .dontAnimate()
                    .apply(new RequestOptions()
                            .transform(new MultiTransformation(new CenterCrop(), new RoundedCorners(radius)))
                            .placeholder(placeImg).error(R.drawable.img_error))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageView);
            return;
        }
        Glide.with(imageView.getContext())
                .asDrawable()
                .skipMemoryCache(skipMemoryCache)
                .load(completeUrl(imageUrl))
                .thumbnail(0.5f)
                .apply(new RequestOptions().placeholder(placeImg).error(R.drawable.img_error))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(imageView);
    }

}
